package com.yuyou.fn.report.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.yuyou.fn.report.entity.RealIncomeOfClass;
import com.yuyou.fn.report.mapper.RealIncomeOfClassMapper;
import com.yuyou.fn.report.service.IRealIncomeOfClassService;
import com.yuyou.fn.report.vo.*;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 * RealIncomeOfClass 表数据服务层接口实现类
 *
 */
@Service
public class RealIncomeOfClassServiceImpl extends SuperServiceImpl<RealIncomeOfClassMapper, RealIncomeOfClass,String> implements IRealIncomeOfClassService {

    @Override
    public List<RealIncomeOfClass> findRealIncomeOfClassByBusTeamIdProductIds(Long busTeamId , List<Long> productIds, Date start , Date end ) {
        Example<RealIncomeOfClass> example = RealIncomeOfClass.createExample();
        example.createCriteria().andEqualTo("busTeamId" , busTeamId ).andIn("productId" , productIds ).andGreaterThanOrEqualTo("effectTime",start).andLessThanOrEqualTo("effectTime",end);
        return findListByExample(example);
    }

    @Override
    public List<RealIncomeOfClass> findRealIncomeOfClassByBusTeamIdGradeSubjectIds(Long busTeamId , List<String> gradeSubjectIds , Date start , Date end ) {
        Example<RealIncomeOfClass> example = RealIncomeOfClass.createExample();
        example.createCriteria().andEqualTo("busTeamId" , busTeamId ).andGreaterThanOrEqualTo("effectTime",start).andLessThanOrEqualTo("effectTime",end);
        List<RealIncomeOfClass> list = findListByExample(example);
        final Set<String> gradeSubjectIdsSet = new HashSet<>();
        for(String s : gradeSubjectIds ){
            gradeSubjectIdsSet.add(s);
        }
        return new ArrayList<>(Collections2.filter(list, new Predicate<RealIncomeOfClass>() {
            @Override
            public boolean apply(RealIncomeOfClass input) {
                return gradeSubjectIdsSet.contains(input.getGradeId() +"#" + input.getSubjectId() );
            }
        }));
    }


    @Override
    public List<RealIncomeOfClass> findRealIncomeOfClassByBusTeamIdsProductIds(List<Long> busTeamIds, List<Long> productIds, Date startDate, Date endDate) {
        if (busTeamIds==null || busTeamIds.size()<=0 || productIds==null || productIds.size()<=0){
            return Collections.EMPTY_LIST;
        }
        Example<RealIncomeOfClass> example = RealIncomeOfClass.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andIn("busTeamId",busTeamIds);
        criteria.andIn("productId",productIds);
        criteria.andGreaterThanOrEqualTo("effectTime",startDate);
        criteria.andLessThanOrEqualTo("effectTime",endDate);
        return this.findListByExample(example);
    }

    @Override
    public List<RealIncomeOfClass> findRealIncomeOfClassByBusTeamIdsGradeIdsSubjectIds(List<Long> busTeamIds, List<Long> gradeIds, List<Long> subjectIds, Date startDate, Date endDate,String ...columns) {
        if (busTeamIds==null || busTeamIds.size()<=0 || gradeIds==null || gradeIds.size()<=0 || subjectIds==null || subjectIds.size()<=0){
            return Collections.EMPTY_LIST;
        }
        Example<RealIncomeOfClass> example = RealIncomeOfClass.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andIn("busTeamId",busTeamIds);
        criteria.andIn("gradeId",gradeIds);
        criteria.andIn("subjectId",subjectIds);
        criteria.andGreaterThanOrEqualTo("effectTime",startDate);
        criteria.andLessThanOrEqualTo("effectTime",endDate);
        return this.findListByExample(example);
    }

    @Override
    public List<TimeAmountVo> findClassTimeAmount(RealIncomeClassQueryVo realIncomeClassQueryVo) {
        return autoMapper.findClassTimeAmount(realIncomeClassQueryVo);
    }

    @Override
    public List<ProductIncomeVo> findProductIncome(RealIncomeClassQueryVo realIncomeClassQueryVo) {
        return autoMapper.findProductIncome(realIncomeClassQueryVo);
    }

    @Override
    public List<GradeSubjectIncomeVo> findGradeSubjectIncome(RealIncomeClassQueryVo realIncomeClassQueryVo) {
        return autoMapper.findGradeSubjectIncome(realIncomeClassQueryVo);
    }

    @Override
    public List<ClassAmountVo> findClassAmount(RealIncomeClassQueryVo realIncomeClassQueryVo) {
        return autoMapper.findClassAmount(realIncomeClassQueryVo);
    }
}